package com.szxy.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

/**
 * @Author: zwz
 * @Date:2020/7/15 21:39
 * @Description:com.szxy.service.impl
 * @Version:1.0
 **/
@Service
public class AccountSereviceApi {

    @Autowired
    private RestTemplate restTemplate;

    /**
     * 调用远程用户服务
     * @param userId 用户 ID
     * @param money  消费金额
     * @return
     */
    public ResponseEntity<Void> deBit(String userId, int money) {
        // 发起请求，调用远程服务
        ResponseEntity<Void> resultEntity = this.restTemplate.getForEntity(
                "http://account-service/deBin/{userId}/{money}",
                Void.class,
                userId,
                money);
        if (resultEntity.getStatusCode() != HttpStatus.OK) {
            throw new RuntimeException("调用远程 account 服务发生异常 ...");
        }
        return resultEntity;
    }

}
